
#include "view_time_8.h"
#include "view_time_8.inc"
#include "view_time_0.h"
#include "display_ctrl.h"
#include "platform_assert.h"
#include "lfs_file_manager_auto.h"
#include "rtc.h"
#include "datetime.h"
#include "blecom.h"
#include "battery_ctrl.h"
#include "sport_his.h"
#include "display_ctrl.h"
#include "display_interface_240x240.h"
#include "datetime.h"
#include "heart_rate_interface.h"
#include "font/num_font.h"
#include "font_mngr.h"
#include "view_common.h"
#include "app_trace.h"
#include "view_times.h"
#include "math.h"
#include "weather_ctrl.h"
//       disp_ctrl.com_data.i_temp[16]  calorie
//  disp_ctrl.com_data.i_temp[11]   step

void view_time_show_temperature_float_with_font(int x, int y, float temperature, bool is_centigrade, bool is_center_align, const unsigned char *const *image_tab, int font_interval, int point_interval,
                                                int unit_interval)
{
    Ensures(temperature >= 0);

    // const unsigned char *const *image_tab = num_16x18_TAB;
    int const font_width = image_tab[0][0] + (image_tab[0][1] << 8);
    int const font_height = image_tab[0][2] + (image_tab[0][3] << 8);
    int const interval = font_interval;
    int draw_x = 0;
    if (isnan(temperature))
    {
        if (is_center_align)
        {
            x += 26;
        }
        disp_draw_rect_color(x, y + font_height / 2 - 2, x + font_width - 1, y + font_height / 2, RGB565_COLOR_WHITE);
        x += font_width + interval;
        disp_draw_rect_color(x, y + font_height / 2 - 2, x + font_width - 1, y + font_height / 2, RGB565_COLOR_WHITE);
        return;
    }
    if (!is_centigrade)
    {
        temperature = 32.f + temperature * 1.8f;
    }
    // float interger_part = 0.f;
    // float decimal_part = 0.f;

    // decimal_part = modff(temperature, &interger_part);
    int temperature_int = temperature * 100;
    if (temperature < 0)
    {
        disp_draw_rect_color(x, y + font_height / 2 - 1, x + font_width - 1, y + font_height / 2, RGB565_COLOR_WHITE);
        x += font_width + interval;
    }
    draw_x = disp_draw_user_image_num_filter_color(x, y, temperature_int / 100, font_width + interval, image_tab);
    draw_x -= interval;
    draw_x += point_interval;
    disp_draw_rect_color(draw_x, y + font_height - 3, draw_x + 3 - 1, y + font_height - 1, RGB565_COLOR_WHITE);
    draw_x += 3 + point_interval;
    draw_x = disp_draw_user_image_num_with_digits_filter_color(draw_x, y, temperature_int % 100, 2, font_width + interval, image_tab);
    draw_x -= interval;
    draw_x += unit_interval;
    // draw_x += 6;
    if (is_centigrade)
    {
        disp_draw_user_image_filter_color(draw_x, y, icon_centigrade);
    }
    else
    {
        disp_draw_user_image_filter_color(draw_x, y, icon_Fahrenheit);
    }
}
void view_time_show_temperature_float(int x, int y, float temperature, bool is_centigrade, bool is_center_align)
{
    view_time_show_temperature_float_with_font(x, y, temperature, is_centigrade, is_center_align, num_16x18_TAB, 1, 1, 7);
}
#ifdef TIME_8_SKIP
#else
void view_time_8_display(void)
{

    _disp_show_flash_img(DISP_LFS_FILE_INDEX_5, 0, 0, FILE_IMG_t100_time_back_0_0);
    show_time_0_battery(disp_ctrl.com_data.i_temp[7], disp_ctrl.com_data.i_temp[8]);
    show_time_0_ble();

    int y = 33;
    int x = 8;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_t101_time_0_8_33 + disp_ctrl.com_data.i_temp[3] / 10);
    x += 36;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, x, y, FILE_IMG_t101_time_0_8_33 + disp_ctrl.com_data.i_temp[3] % 10);
    x += 36;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_4, x, y, FILE_IMG_t101_time_second_8_33);
    x += 21;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_2, x, y, FILE_IMG_t101_time_0_8_33 + disp_ctrl.com_data.i_temp[4] / 10);
    x += 36;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_3, x, y, FILE_IMG_t101_time_0_8_33 + disp_ctrl.com_data.i_temp[4] % 10);
// #warning debug
//  disp_ctrl.com_data.f_temp[26] = 37.25f;
#ifdef ITALIAN_DATE_ON_TIME
    int month = disp_ctrl.com_data.i_temp[1] - 1;
    int day = disp_ctrl.com_data.i_temp[2];
    int weeks = disp_ctrl.com_data.i_temp[5] - 1;
    uint8_t const *icon_month = italian_moon_TAB[month];
    uint8_t const *icon_week = italian_week_TAB[weeks];
#if 1
    uint16_t const base_color = RGB565_COLOR_WHITE;
#else
#warning debug
    uint16_t const base_color = 0xe720;
#endif
    int date_x = 6;
    int date_y = 102;
    disp_draw_user_image_filter_color_gray_lucency(date_x, date_y, icon_month, base_color);
    date_x += icon_month[0] + (icon_month[1] << 8) + 3;
    disp_draw_user_image_filter_color_gray_lucency(date_x, date_y, slash_8x15, base_color);
    date_x += slash_8x15[0] + (slash_8x15[1] << 8) + 3;
    disp_draw_user_image_filter_color_gray_lucency(date_x, date_y, num_11x15_TAB[day / 10], base_color);
    date_x += num_11x15_TAB[0][0] + (num_11x15_TAB[0][1] << 8) + 1;
    disp_draw_user_image_filter_color_gray_lucency(date_x, date_y, num_11x15_TAB[day % 10], base_color);
    date_x += num_11x15_TAB[0][0] + (num_11x15_TAB[0][1] << 8) + 3;
    disp_draw_user_image_filter_color_gray_lucency(date_x, date_y, icon_week, base_color);
    date_x += icon_week[0] + (icon_week[1] << 8) + 1;
    view_time_show_temperature_float(3, 217, disp_ctrl.com_data.f_temp[26], disp_ctrl.com_data.i_temp[21] == WEATHER_TEMPERATURE_TYPE_C, true);

    uint32_t heart_rate_digits = 0;
    if (disp_ctrl.com_data.i_temp[17] < 40 || disp_ctrl.com_data.i_temp[17] > 220)
    {
        heart_rate_digits = 2;
    }
    else
    {
        heart_rate_digits = get_num_digit(disp_ctrl.com_data.i_temp[17]);
    }
    int const heart_rate_num_width = 16;
    int const heart_rate_num_height = 18;
    int const heart_rate_num_interval = 1;
    int const heart_rate_start_x = 192;
    int const heart_rate_size = 3 * heart_rate_num_width + 2 * heart_rate_num_interval;
    int heart_rate_draw_x = heart_rate_start_x + (heart_rate_size - heart_rate_digits * heart_rate_num_width - (heart_rate_digits - 1) * heart_rate_num_interval) / 2;
    int const heart_rate_draw_y = 216;
    if (disp_ctrl.com_data.i_temp[17] < 40 || disp_ctrl.com_data.i_temp[17] > 220)
    {
        disp_draw_rect_color(heart_rate_draw_x, heart_rate_draw_y + heart_rate_num_height / 2 - 3, heart_rate_draw_x + heart_rate_num_width - 1, heart_rate_draw_y + heart_rate_num_height / 2 - 1,
                             RGB565_COLOR_WHITE);
        heart_rate_draw_x += heart_rate_num_width + heart_rate_num_interval;
        disp_draw_rect_color(heart_rate_draw_x, heart_rate_draw_y + heart_rate_num_height / 2 - 3, heart_rate_draw_x + heart_rate_num_width - 1, heart_rate_draw_y + heart_rate_num_height / 2 - 1,
                             RGB565_COLOR_WHITE);
    }
    else
    {
        disp_draw_user_image_num_filter_color(heart_rate_draw_x, heart_rate_draw_y, disp_ctrl.com_data.i_temp[17], heart_rate_num_width + heart_rate_num_interval, num_16x18_TAB);
    }
#else
#ifdef TIME_NO_WRIST_TEMPERATURE
    view_time_show_temperature_float(37, 129, disp_ctrl.com_data.f_temp[26], disp_ctrl.com_data.i_temp[21] == WEATHER_TEMPERATURE_TYPE_C, false);
#else
    view_time_show_temperature_float(37, 129, disp_ctrl.com_data.f_temp[25], disp_ctrl.com_data.i_temp[21] == WEATHER_TEMPERATURE_TYPE_C, false);
    view_time_show_temperature_float(37, 171, disp_ctrl.com_data.f_temp[26], disp_ctrl.com_data.i_temp[21] == WEATHER_TEMPERATURE_TYPE_C, false);
#endif
    int const heart_rate_num_width = 16;
    int const heart_rate_num_height = 18;
    int const heart_rate_num_interval = 1;
    int const heart_rate_draw_y = 216;
    int heart_rate_draw_x = 237 - 2 * heart_rate_num_width - heart_rate_num_interval;
    if (disp_ctrl.com_data.i_temp[17] < 40 || disp_ctrl.com_data.i_temp[17] > 220)
    {
        disp_draw_rect_color(heart_rate_draw_x, heart_rate_draw_y + heart_rate_num_height / 2 - 3, heart_rate_draw_x + heart_rate_num_width - 1, heart_rate_draw_y + heart_rate_num_height / 2 - 1,
                             RGB565_COLOR_WHITE);
        heart_rate_draw_x += heart_rate_num_width + heart_rate_num_interval;
        disp_draw_rect_color(heart_rate_draw_x, heart_rate_draw_y + heart_rate_num_height / 2 - 3, heart_rate_draw_x + heart_rate_num_width - 1, heart_rate_draw_y + heart_rate_num_height / 2 - 1,
                             RGB565_COLOR_WHITE);
    }
    else
    {
        disp_draw_user_image_num_right_align_filter_color(237, heart_rate_draw_y, disp_ctrl.com_data.i_temp[17], 1, num_16x18_TAB);
    }
#endif
}
#endif
